package phapp.service.system.impl;

import static javacommon.util.Struts2JsonHelper.outJson;

import java.io.IOException;
import java.sql.Connection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javacommon.util.SpringContextUtil;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;
import javacommon.base.*;

import phapp.model.system.*;
import phapp.dao.system.*;
import phapp.service.system.impl.*;
import phapp.service.system.*;

/**
 * Title: ts_menu_info 业务处理层 Description: spring事务实现层,调用Dao层取数据 Copyright:
 * Copyright (c) 2012-10 Company:
 * 
 * @author you name
 * @version 1.0
 */

@Component("menuManager")
@Transactional
public class MenuManagerImpl extends BaseManager<Menu, java.lang.Integer> implements MenuManager
{

   /** 定义Dao */
   private MenuDao menuDao;

   /** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性 */
   public void setMenuDao(MenuDao dao)
   {
      this.menuDao = dao;
   }

   /** 获取Dao */
   public EntityDao getEntityDao()
   {
      return this.menuDao;
   }

   /**
    * <p>
    * 分页查询Menu<br>
    * 
    * @param PageRequest
    *           pr
    * @return Page
    * @author
    */
   @Transactional(readOnly = true)
   public Page findByPageRequest(PageRequest pr)
   {
      return menuDao.findByPageRequest(pr);
   }

   @Transactional(readOnly = false)
   public Boolean removeByIdArry(Menu u)
   {
      Boolean result = false;
      int i = -1, size = u.getIdList().size();
      i = menuDao.removeByIdArry(u);
      if (i == size)
      {
         result = true;
      }
      else
      {
         throw new RuntimeException();
      }
      return result;
   }

   
   @Transactional(readOnly = true)
   public JSONObject findUserSubmenu(int UserID, int MenuID)
   {
      List<Menu> listQuery = null;
      JSONArray jsonSubArray = null;
      int i = 0, nCount = 0, nSubMenuID = 0;
      Menu menuItem = null, submenuItem = null;
      Map<String, Object> mapQuery = new HashMap<String, Object>();
      JSONObject jsonMenu = null, jsonSubMenu = null, jsonAttr = null;

      try
      {
         menuItem = (Menu) menuDao.getById(new Integer(MenuID));
         if(menuItem != null)
         {
            jsonMenu = new JSONObject();
            jsonAttr = new JSONObject();
            
            jsonMenu.put("id", menuItem.getId());
            jsonMenu.put("text", menuItem.getFuncName());
            //jsonMenu.put("iconCls", mapResult.get("iconCls"));
            jsonAttr.put("url", menuItem.getFuncUrl());
            jsonMenu.put("attributes", jsonAttr);

            mapQuery.clear();
            if(UserID > 0)
               mapQuery.put("userId", UserID);
            mapQuery.put("funcPid", MenuID);
            listQuery = menuDao.findUserSubmenuList(mapQuery);
            if(listQuery.size() > 0)
            {
               jsonSubArray = new JSONArray();
               nCount = listQuery.size();
               for(i = 0; i < nCount; i++)
               {
                  submenuItem = (Menu) listQuery.get(i);
                  nSubMenuID = submenuItem.getId().intValue();

                  jsonSubMenu = findUserSubmenu(UserID, nSubMenuID);
                  if(jsonSubMenu != null)   jsonSubArray.add(jsonSubMenu);
               }
            }

            if(jsonSubArray != null && jsonSubArray.size() > 0)
            {
               jsonMenu.put("state", "open");
               jsonMenu.put("children", jsonSubArray);
            }
         }
      }
      catch (Exception eEnum)
      {
         jsonMenu = null;
         eEnum.printStackTrace();
      }
      finally
      {
         if(listQuery != null) listQuery.clear();
         if(mapQuery != null) mapQuery.clear();

         listQuery = null;
         mapQuery = null;
      }

      return jsonMenu;
   }

}
